/**
 * Created by Administrator on 2018/12/29.
 */
var table;
var parentCode;
$(function(){
    table = $("#mouldList");
    parentCode = locations.ParamString("parentCode");
    initComponent();
    initGrid();
    queryMouldFunction();
    fancyInit();
    /*var pager = $('#dg').datagrid().datagrid('getPager');	// get the pager of datagrid
    pager.pagination({
        buttons:[{
            iconCls:'icon-search',
            handler:function(){
                alert('search');
            }
        },{
            iconCls:'icon-add',
            handler:function(){
                alert('add');
            }
        },{
            iconCls:'icon-edit',
            handler:function(){
                alert('edit');
            }
        }]
    });*/
});

function initComponent() {
    var curr_time = new Date();
    var strDate = curr_time.getFullYear() + "-";
    strDate += curr_time.getMonth() + 1 + "-";
    strDate += curr_time.getDate() + " 00:00:00";
    $("#startTime").datetimebox("setValue", strDate);
    $("#endTime").datetimebox("setValue", addDate(curr_time, 0) + " 23:59:59");
}

function initGrid(){
    var param = queryButtonLimit(parentCode);
    table.datagrid({
        iconCls : 'icon-ok',
        pageSize : 10,
        pageList : [ 10, 20, 40, 80 ],
        striped : true,
        collapsible : false,
        remoteSort : true,// 服务器端排序
        fitColumns : false,
        singleSelect : true,
        showFooter : true,
        columns : [[{
            field : 'id',
            checkbox : true,
            hidden:true
            }, {
                field : 'mouldNo',
                title : '模具编号',
                width : 150,
                align : 'center'
            }, {
            field : 'mouldColor',
            title : '模具颜色',
            width : 120,
            align : 'center'
            }, {
            field : 'mouldName',
            title : '模具名称',
            width : 200,
            align : 'center'
            }, {
            field : 'spec',
            title : '模具规格',
            width : 120,
            align : 'center'
            }, {
            field : 'entryNickname',
            title : '创建人',
            width : 120,
            align : 'center'
            }, {
            field : 'entryTime',
            title : '创建时间',
            width : 200,
            align : 'center',
            formatter:function (value, row, index) {
                if(row.entryTime == null) {
                    return "";
                }
                return dateFtt("yyyy-MM-dd hh:mm:ss",new Date(row.entryTime));
            }
            }
            /*,{
            field : 'picAddr',
            title : '图示',
            width : 160,
            align : 'center',
            formatter:function (value, row, index) {
                if(value != null && value!= undefined && value != "" && value != "/sh") {
                    return "<a class=\"fancybox-buttons\" href=\""+value+
                        "\" data-fancybox-group=\"button\" ><img src=\""+value+
                        "\" style=\"height:50px;\"/></a>";
                } else {
                    return "";
                }
            }
            }*/
        ]],
        pagination : true,
        rownumbers : true,
        toolbar : param,
        onBeforeLoad : function(param) {
            var parameter = new Object();
            parameter.page = param.page;
            parameter.rows = param.rows;
            if($("#mouldNo").val()!="") parameter.mouldNo = $("#mouldNo").val();
            if($("#mouldColor").val()!="") parameter.mouldColor = $("#mouldColor").val();
            if($("#mouldName").val()!="") parameter.mouldName = $("#mouldName").val();
            if($("#spec").val()!="") parameter.spec = $("#spec").val();
            if($("#startTime").val()!="") parameter.startTime = $("#startTime").val();
            if($("#endTime").val()!="") parameter.endTime = $("#endTime").val();
            var jsonstr = postSyncAjax("/selectMouldByPage", parameter);
            //console.log(jsonstr);
            //console.log(jsonstr.data);
            var json = jsonstr.data;
            var dataGridData = new Object();
            dataGridData.rows = json.rows;
            dataGridData.total = json.total;
            table.datagrid('loadData', dataGridData);
        }
    });
}
// 查询和重置
var queryMouldFunction = function () {
    $("#queryMouldBtn").click(function () {
        table.datagrid("reload");
    });
    $("#restoreBtn").click(function () {
        $("form[name='queryForm']").form('reset');
        initComponent();
        table.datagrid("reload");
    });
}

// 新增
var addMould = function (uri) {
    showThePage(parentCode,uri);
}
//删除
var removeMould = function(uri){
    var rows = table.datagrid('getSelected');
    if (null == rows) {
        $.messager.alert("消息提示", "至少选择一条记录", "warning");
        return;
    } else {
        $.messager.confirm("确认对话框","确定要删除该模具信息？",function (r) {
            if(r){
                var parameter = new Object();
                parameter.id = rows.id;
                var jsonstr = postSyncAjax(uri, parameter);
                //console.log(jsonstr);
                if(jsonstr.success) {
                    //$.messager.alert("消息提示", "模具删除成功！", "warning");
                    //table.datagrid("reload");
                    showDialogWithFunc("模具删除成功("+jsonstr.msg+")！",function () {
                        table.datagrid("reload");
                     });
                } else {
                    //$.messager.alert("消息提示", "模具删除成功！", "warning");
                    showDialogWithContent("模具删除失败："+jsonstr.msg);
                }
            }
        });
    }
}

// 修改模具
var editMould = function (uri) {
    var rows = table.datagrid('getSelected');
    if (null == rows) {
        $.messager.alert("消息提示", "至少选择一条记录", "warning");
        return;
    }

    //top.$('#cur_id').val(rows.id);
    //console.log(rows.id);
    //removeTabs('sh-tabs','修改模具');
    //createTabs('sh-tabs','/editMould/normal/'+rows.id,'修改模具','menu3');
    var id = rows.id;
    showThePage(parentCode, uri, {
        url : uri + '/' + id
    });
}

// 模具详情
var mouldDetail = function (uri) {
    var rows = table.datagrid('getSelected');
    if (null == rows) {
        $.messager.alert("消息提示", "至少选择一条记录", "warning");
        return;
    }
    showThePage(parentCode, uri, {
        url : uri + '/' + rows.id
    });
    //top.$('#cur_id').val(rows.id);
    //console.log("ceshi:"+rows.id);
    //removeTabs('sh-tabs','模具详情');
    //createTabs('sh-tabs','/mouldDetail/normal/'+rows.id,'模具详情','menu4');
    //openMessageZone('/mouldDetail/normal/'+rows.id);
}

var fancyInit = function () {
    $('.fancybox-buttons').fancybox({
        openEffect  : 'none',
        closeEffect : 'none',

        prevEffect : 'none',
        nextEffect : 'none',

        closeBtn  : false,

        helpers : {
            title : {
                type : 'inside'
            },
            buttons	: {}
        },

        afterLoad : function() {
            this.title = 'Image ' + (this.index + 1) + ' of ' + this.group.length + (this.title ? ' - ' + this.title : '');
        }
    });
}